package a07_二叉树;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * a01_二叉树的前序遍历
 * </p>
 *
 * @author flyduck
 * @since 2024-08-19
 */
public class a02_二叉树的递归遍历复习1 {

    public static void main(String[] args) {
        TreeNode treeNode = new TreeNode(1, null, new TreeNode(2, new TreeNode(3, null, null), null));
        List<Integer> result = new ArrayList<>();
        preorderTraversal(treeNode,result);
        System.out.println(result);

    }

    //前序
    public static void preorderTraversal(TreeNode root,List<Integer> result) {
        if(root == null){
            return;
        }
        //中
        result.add(root.val);
        //左
        preorderTraversal(root.left, result);
        //右
        preorderTraversal(root.right, result);
    }

    //中序
    public static void inorderTraversal(TreeNode root,List<Integer> result) {
        if(root == null){
            return;
        }
        //左
        inorderTraversal(root.left, result);
        //中
        result.add(root.val);
        //右
        inorderTraversal(root.right, result);
    }

    //后序
    public static void postorderTraversal(TreeNode root,List<Integer> result) {
        if(root == null){
            return;
        }
        //左
        postorderTraversal(root.left, result);
        //右
        postorderTraversal(root.right, result);
        //中
        result.add(root.val);
    }


}
